Issue with seed-based generation [C#]

Hello everybody,

after tinkering around with seed-based generation, i have run into an issue. I use part of a float and turn it into a string, but the string always becomes something along the lines of 2.090868e+18 which for me is useless, because i use a float to generate boxes in a specific location. After a couple of boxes being spawned, i obviously run into the problem of reaching a letter instead of a number, and i have not a single clue as of how i can fix this. I hope you guys do.
Code:

using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;

public class gameSystems : MonoBehaviour {
    
    public Transform boxToSpawn;

    public float timeLeft;
    

    public int score;
    public AudioClip pickupAudio;


    /////////BOX SPAWNING VARIABLES/////////////
    public float generatedSeed;
    public string seedTempString;
    public char seedChar;
    public string seedCharString;
    public int boxesGenerated;
    public float boxSpawnPos;
    public float boxPreviousSpawnPos;


    void Awake()
    {
        GameObject.Find("gameSystems").SendMessage("SaveGame", SendMessageOptions.DontRequireReceiver);
        DontDestroyOnLoad(transform.gameObject);
        boxesGenerated = 1;
        GenerateSeed();
        CalculateSpawnPos();
        Time.timeScale = 1;
         }



    

    
    

    

    void AddScore(int scoreAdded)
    {
        audio.PlayOneShot(pickupAudio);
        score += scoreAdded;
        CalculateSpawnPos();
        {
            GameObject.Find("gameSystems").SendMessage("SaveGame", SendMessageOptions.DontRequireReceiver);
        }
        





    }


    void GenerateSeed()
    {

        generatedSeed = Random.Range(1000000000000000000, 10000000000000000000);

    }

    void CalculateSpawnPos()
    {

       
        if (boxSpawnPos == 0) 
        {
            boxSpawnPos = 1;
            
        }
        if (boxSpawnPos == boxPreviousSpawnPos) 
        { 
            boxSpawnPos += 1;
            
        }
        spawnBox(); 
    }



    void FixedUpdate()
    {
        seedTempString = generatedSeed.ToString();
        seedChar = seedTempString[boxesGenerated];
        seedCharString = seedChar.ToString();
        boxSpawnPos = float.Parse(seedCharString);
        timeLeft -= Time.deltaTime;
        if (timeLeft <= 0) { Application.LoadLevel(Application.loadedLevel); }
    }

    

    void spawnBox()
    {
        
        
            boxesGenerated += 1;
            if (boxSpawnPos == 1) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn1").transform.position, Quaternion.identity); }
            if (boxSpawnPos == 2) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn2").transform.position, Quaternion.identity); }
            if (boxSpawnPos == 3) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn3").transform.position, Quaternion.identity); }
            if (boxSpawnPos == 4) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn4").transform.position, Quaternion.identity); }
            if (boxSpawnPos == 5) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn5").transform.position, Quaternion.identity); }
            if (boxSpawnPos == 6) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn6").transform.position, Quaternion.identity); }
            if (boxSpawnPos == 7) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn7").transform.position, Quaternion.identity); }
            if (boxSpawnPos == 8) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn8").transform.position, Quaternion.identity); }
            if (boxSpawnPos == 9) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn9").transform.position, Quaternion.identity); }
            boxPreviousSpawnPos = boxSpawnPos;
        }
                
        
    }

Thank you for taking the time to help me, and i hope i can at some point help you.

Greetings,

Roel Zwakman / RoboticSarcasm

you mean e-18?

Long Answer:

what that means is there are 18 0’s before the 2. knowing that you could write a calculation to see if

if (yourString.SubString(yourString.length - 2, 1) == "-") {
   //this means you have a *****e-** string
}
else if (yourString.SubString(yourString.length - 1, 1) == "-") {
   //this means you have a *****e-* string
}

and if either of those are true make a string with whatever the yourString ends in, for example 18, zeros you could that with

for (int i = 0; i < zeroCount; i++) {
   newString += "0";
}

and then append the 2, skip the period, and add the rest.

Short Answer:

with a number that small though, I think it’s trying to tell you that you’re at zero =D

The e is not useless, its a way to display a float, read about it here. Are more mathematically correct explanation can be found here

Now to fix your problem, the simple solution would be to use

seedChar.ToString("F");

But this will turn your number into a big string with trailing/leading zeros. Which is correct, because you want to generate numbers way bigger than possible. Your max number for example is 10.000.000.000.000.000.000 which reads 10 quintillion. Way more than a computer can conventionally store or work with. Ask yourself why you would need such a big number and why it must be float.

Edit: “F” is correct, not “#”.

http://stackoverflow.com/questions/3757945/how-to-get-floats-value-without-including-exponential-notation

You can pass a parameter to the ToString method:

seedTempString = generatedSeed.ToString("F");

As a side note you would probably want to have an array of spawn points rather than using the Find() method for performance reasons.
Also you could simplify the spawnBox code to:

void spawnBox()
{
   boxesGenerated++;
   Instantiate(boxToSpawn, GameObject.Find("boxSpawn" + boxSpawnPos).transform.position, Quaternion.identity);
   boxPreviousSpawnPos = boxSpawnPos;
}

It seems like you’d want to move the boxSpawnPos calculations from the FixedUpdate function to the CalculateSpawnPos method, since this doesn’t need to be calculated every frame.
Anyway, I hope this solves your question about storing a float value in a string :slight_smile: