1

I want to do an INSER INTO statement but i need some fields to generate random numbers each time: Example

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,RAND NUMBER,RAND NUMBER,...);

So each time it enters a random number into columns when creating a new record. SO most values will be static but some need to be RAND between a number set for example 1 -5 or 1000 - 5000.

Any help?

Speedychuck
  • 400
  • 9
  • 29
  • [This link](http://www.techonthenet.com/mysql/functions/rand.php) shows you how to use `RAND()` to generate a random number within a range. You can just plug this into your `INSERT` statement. – Tim Biegeleisen Jul 22 '16 at 14:30
  • I tried that so in the values I have VALUES ('511222', RAND()*(5-1)+1, '323632') Because I want numbers between 1 and 5. using MYSQL work bench not sure if that makes a difference – Speedychuck Jul 22 '16 at 14:31
  • @Speedychuck did that not work ? – apokryfos Jul 22 '16 at 14:46

3 Answers3

2

Using this function should work:

FLOOR(min+RAND()*(max-min))

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,SELECT FLOOR(5+RAND()*(1-5)),SELECT FLOOR(5+RAND()*(1-5)),...);
Zoidburg
  • 159
  • 8
1

Insert some rows with random numbers from 1 to 5 in some columns.

Schema:

create table friday1
(   id int auto_increment primary key,
    value1 int not null,
    value2 int not null
);

Insert 3 rows:

insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;

Stored Proc helper:

drop procedure if exists insertMany;
DELIMITER $$
create procedure insertMany
(   howMany int
)
BEGIN
    DECLARE soFar int default 0;

    set howMany=least(howMany,500); -- max out at 500 regardless of IN parameter (RAND is slow)
    WHILE soFar<howMany DO
        insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
        set soFar=soFar+1;
    END WHILE;
    select soFar; -- # of times in loop
END$$
DELIMITER ; 

Test:

call insertMany(200);

Results:

select count(*) as theCount, 
least(min(value1),min(value2)) as theMin, 
greatest(max(value1),max(value2)) as theMax 
from friday1; 

+----------+--------+--------+
| theCount | theMin | theMax |
+----------+--------+--------+
|      203 |      1 |      5 |
+----------+--------+--------+
Drew
  • 24,851
  • 10
  • 43
  • 78
0

Take a look at this.

You can use RAND(), but as it says in the documentation,

If seed is not specified, the SQL Server Database Engine assigns a seed value at random. For a specified seed value, the result returned is always the same.

Hope this helps you

EDIT:

So if you want to use it in the INSERT INTO....i assume

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, RAND(CAST( NEWID() AS varbinary )) * 5, RAND(CAST( NEWID() AS varbinary )) * 1000,...)

If this doesnt help, thumbs down as I'm going from what I'd try first and im not testing

Alex Grogan
  • 158
  • 1
  • 1
  • 11
  • Need to know how to use it in an INSERT INTO :( so when I use the insert it will Generate the number between 1 and 5 for that column – Speedychuck Jul 22 '16 at 14:41
  • Look at my edit, try it out....youll need to round the number if you want a whole number – Alex Grogan Jul 22 '16 at 14:46
  • CAST(FLOOR(5+RAND()*(1-5)) AS CHAR) Now works, however when I check what the column accepts it is a TINYINT. So FLOOR RAND generates the number between 1 and 5 but wont insert it into the column becasue its trying to add an INT which is then converted into a String. How do I add as TINYINT is my issues. @Alex Grogan – Speedychuck Jul 22 '16 at 16:05
  • Do you really want to use a TINYINT? It only has a range of 0-255? You're betting off changing the design of the table so its an INT instead – Alex Grogan Jul 25 '16 at 15:56